package com.fitz.remote.client.pool;

import com.fitz.common.LocalCache;
import com.fitz.registry.InstanceInfo;
import com.fitz.remote.client.ConsumerClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * @author fitz
 * @date 2020/8/17 16:17
 */
public class InitClientPool {

    private static final Logger logger = LoggerFactory.getLogger(InitClientPool.class);

    public static void initClientPool(int minPoolSize,int maxPoolSize,byte serializerType,List<InstanceInfo> serverList){
        logger.info("initClientPool >>>>>>>>>>>>>>");

        for (InstanceInfo instanceInfo:serverList){
            InstanceInfo node = new InstanceInfo(instanceInfo.getHost(), instanceInfo.getPort());
            node.setSerializerType(serializerType);
            ConsumerClientPool clientPool = new ConsumerClientPool(node,minPoolSize,maxPoolSize);
            ConsumerClient client = clientPool.getClient();
            clientPool.returnClient(client);
            logger.info("init getNumIdle poolSize:{}",clientPool.getNumIdle());
            logger.info("init getMaxTotal poolSize:{}",clientPool.getMaxTotal());
            String serverKey = instanceInfo.serverKey();
            LocalCache.getClientPoolMap().put(serverKey,clientPool);
        }
    }
}
